package com.lagou.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.lagou.dao.LagouAuthCodeDao;
import com.lagou.pojo.LagouAuthCode;
import com.lagou.service.CodeService;
import com.lagou.service.EmailService;
import com.lagou.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Service
public class CodeServiceImpl implements CodeService {

    @Reference(check = false)
    private EmailService emailService;
    @Reference(check = false)
    private UserService userService;

    @Autowired
    private LagouAuthCodeDao lagouAuthCodeDao;

    /**
     * 生成验证码，随机六位数
     *
     * @param email
     * @return
     */
    @Override
    public boolean createCode(String email) {
        if (userService.isRegistered(email)) {
            return false; // 已经注册过
        }
        try {
            // 生成验证码，存储到数据库，并发送到邮箱
            int code = (int) ((Math.random() * 9 + 1) * 100000);
            String codeStr = String.valueOf(code);
            // TODO 存入数据库
            LagouAuthCode lagouAuthCode = new LagouAuthCode();
            lagouAuthCode.setCode(codeStr);
            lagouAuthCode.setEmail(email);
            long currentTime = System.currentTimeMillis();
            lagouAuthCode.setCreatetime(new Date());
            DateTime dateTime = DateUtil.offsetSecond(new Date(), 20);
            lagouAuthCode.setExpiretime(dateTime.toSqlDate());
            lagouAuthCodeDao.save(lagouAuthCode);

            // 调用邮件微服务发送邮件
            emailService.sendSimpleMail(email, "AuthCode", codeStr);
            System.out.println("===============>>>>>验证码微服务，处理正常.....");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询最近一次为该邮箱生成的验证码，校验验证码是否正确，0正确1错误2超时
     *
     * @param email
     * @param code
     * @return
     */
    @Override
    public String validateCode(String email, String code) {
        LagouAuthCode latestCode = lagouAuthCodeDao.findLatestCode(email);
        if (latestCode == null) {
            return "1";
        }
        long expireTime = latestCode.getExpiretime().getTime();
        long nowTime = System.currentTimeMillis();
        if (nowTime > expireTime) {
            return "2";
        }
        return "0";
    }
}
